import { useState } from 'react';
import type { DiaConfig } from '@/utils/aurora-dia';
import { AuroraDia } from '@/utils/aurora-dia';

interface DiaStoreType {
  dia: {
    configs: DiaConfig;
    software: any;
    eyesAnimationTimer: number | undefined;
    installSoftware: (configs: DiaConfig) => void;
    on: () => void;
    activateMotion: () => void;
  };
  aurora_bot: {
    enable: boolean;
    locale: string;
    bot_type: string;
    tips?: Record<string, { selector: string; text: string | string[] }>;
  };
}

// Partial 所有类型变成可选类型
export default () => {
  const [isMobile, setIsMobile] = useState<boolean>(false);
  const [diaStore] = useState<DiaStoreType>({
    dia: new AuroraDia(),
    aurora_bot: {
      enable: true,
      locale: 'cn',
      bot_type: 'dia',
    },
  });

  // 判断当前屏幕尺寸是否为手机
  const resizeHander = () => {
    const rect = document.body.getBoundingClientRect();
    const mobileState = rect.width - 1 < MOBILE_WITH;
    if (isMobile !== mobileState) setIsMobile(mobileState);
  };

  const initResizeEvent = () => {
    resizeHander();
    window.addEventListener('resize', resizeHander);
  };

  const initializeBot = (configs: DiaConfig): void => {
    diaStore.dia.installSoftware(configs);
    diaStore.dia.on();
  };

  return {
    resizeHander,
    initResizeEvent,
    initializeBot,
    diaStore,
    isMobile,
  };
};
